package com.tianji.remark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.utils.ObjectUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.dto.LikedTimesDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author LYQ
 * @since 2024-06-28
 */
//@Service
@RequiredArgsConstructor
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {
    private final RabbitMqHelper rabbitMqHelper;

    /**
     * 点赞或取消点赞
     *
     * @param likeRecordDTO
     */
    @Override
    public void addLikedRecord(LikeRecordFormDTO likeRecordDTO) {
        //1、判断是点赞还是取消，执行操作并返回是否执行成功
        boolean isSuccess = likeRecordDTO.getLiked() ? like(likeRecordDTO) : undoLike(likeRecordDTO);
        //2、操作失败直接return
        if (!isSuccess) return;
        //3、操作成功查询点赞数量
        Integer likedCount = lambdaQuery().eq(LikedRecord::getBizId, likeRecordDTO.getBizId()).count();
        //4、发送消息到对应微服务，修改点赞数量
        rabbitMqHelper.send(
                LIKE_RECORD_EXCHANGE,
                StringUtils.format(LIKED_TIMES_KEY_TEMPLATE, likeRecordDTO.getBizType()),
                LikedTimesDTO.of(likeRecordDTO.getBizId(), likedCount));
    }

    /**
     * 新增点赞记录
     *
     * @param likeRecordDTO
     * @return
     */
    private boolean like(LikeRecordFormDTO likeRecordDTO) {
        //1、查询点赞记录是否存在
        LikedRecord likedRecord = lambdaQuery()
                .eq(LikedRecord::getUserId, UserContext.getUser())
                .eq(LikedRecord::getBizId, likeRecordDTO.getBizId())
                .one();
        //2、点赞记录存在直接返回执行失败
        if (ObjectUtils.isNotEmpty(likedRecord)) return false;
        //3、点赞记录不存在新增点赞记录
        return save(new LikedRecord()
                .setBizId(likeRecordDTO.getBizId())
                .setUserId(UserContext.getUser())
                .setBizType(likeRecordDTO.getBizType()));
    }

    /**
     * 删除点赞记录
     *
     * @param likeRecordDTO
     * @return
     */
    private boolean undoLike(LikeRecordFormDTO likeRecordDTO) {
        //1、直接删除
        return remove(new LambdaQueryWrapper<LikedRecord>()
                .eq(LikedRecord::getUserId, UserContext.getUser())
                .eq(LikedRecord::getBizId, likeRecordDTO.getBizId()));
    }

    /**
     * 根据业务ID集合查询是否已经点赞
     *
     * @param bizIds
     */
    @Override
    public Set<Long> isLiked(List<Long> bizIds) {
        //1、根据业务ID集合查询点赞记录
        List<LikedRecord> likedRecords = lambdaQuery()
                .eq(LikedRecord::getUserId, UserContext.getUser())
                .in(LikedRecord::getBizId, bizIds)
                .list();
        //2、steam处理抽取所需字段
        Set<Long> likedList = likedRecords.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
        return likedList;
    }


    @Override
    public void checkLikedTimesAndSendMsg(String bizType ,Integer maxBizSize) {
    }
}
